var HomepageCtrl = dojo.declare(null, {
	//contructor/destructor
	constructor : homepageCtrl_constructor,
	destroy : homepageCtrl_destructor,
	
	//methods
	init : homepageCtrl_init,
	loadLeftNavPage : homepageCtrl_loadLeftNavPage,
	loadMapPage : homepageCtrl_loadMapPage,
	
	//slots
	destroyContentPage : homepageCtrl_destroyContentPage,
	loadContentPage : homepageCtrl_loadContentPage,
	updateLoginState : homepageCtrl_updateLoginState
});

function homepageCtrl_constructor(){
	var obj = this;
	//private functions
	obj.getContentPage_p = homepageCtrl_getContentPage_p;
	
	//private variables
	obj._leftNavFloater = new GlobalFloater({
		z_index : 5,
		hideLook : {left : "100px", top : "100px", width : "200px", height : "50px"},
		loadingLook : {left : "100px", top : "100px", width : "200px", height : "50px"},
		defaultLook : {left : "100px", top : "100px", width : "200px", height : "80px"}
	});
	obj._mapFloater = new GlobalFloater({
		pagePadding: 0,
		z_index : 0,
		hideLook : {left : "0", top : "0", width : $(window).width() + "px", height : ($(window).height() - 20) + "px"},
		defaultLook : {left : "0", top : "0", width : $(window).width() + "px", height : ($(window).height() - 20) + "px"},
		loadingLook : {left : "0", top : "0", width : $(window).width() + "px", height : ($(window).height() - 20) + "px"}
	});
	
	$(window).resize(function(){
		obj._mapFloater.setHideLook(
			{left : "0", top : "0", width : $(window).width() + "px", height : ($(window).height() - 20) + "px"});
		obj._mapFloater.setDefaultLook(
			{left : "0", top : "0", width : $(window).width() + "px", height : ($(window).height() - 20) + "px"});
		obj._mapFloater.setLoadingLook(
			{left : "0", top : "0", width : $(window).width() + "px", height : ($(window).height() - 20) + "px"});
	});
	
	obj._leftNavLoader = null;
	obj._mapLoader = null;
	//Structure of contentPages : [[name, GlobalFloater, PageLoader], ...]
	obj._contentPages = [];
	
	obj._pageBaseFloaterName = {"myloc" : "my"};
	obj._pageJsLoading = {"map" : ["/plugins/jquery_lightbox/js/jquery.lightbox.js", "/script/homepage/mainpage_map.js"],
						  "myloc" : ["/script/homepage/mainpage_myloc.js"]
	};
	obj._pageCSSLoading = {"map" : ["/css/homepage/mainpage_map.css"]};
	obj._pageClassName = {"map" : "mapCtrl", "myloc" : "mylocCtrl"};
	
	obj._connections = new Array();
}

function homepageCtrl_destructor(){
	for (var i = 0; i < _connections.length; i ++)
		dojo.disconnect(this._connections[i]);
}

function homepageCtrl_init(){
	this._connections.push(dojo.connect(globalCtrl, "userLoginStateChanged", this, "updateLoginState"));
	
	this.loadLeftNavPage();
	this.loadMapPage();
}

function homepageCtrl_loadLeftNavPage(){
	var obj = this;
	obj._leftNavFloater.showLoading();
	if (obj._leftNavLoader != null)
		obj._leftNavLoader.destroy();
	obj._leftNavLoader = new PageLoader("/homepage/getLeftNavPage", "leftnavCtrl", 
		{javaScript : ["/script/homepage/leftnav.js"]}, {homepageController : obj}, function(data, callback){
			obj._leftNavFloater.setDefaultHtml(data);
			obj._leftNavFloater.showDefault(callback);
	});
}

function homepageCtrl_loadMapPage(){
	var obj = this;
	var page = "map";
	obj._mapFloater.showLoading();
	this._mapLoader = new PageLoader("/homepage/getMainPage", this._pageClassName[page], 
		{postData : {"page" : page}, javaScript : this._pageJsLoading[page], CSS : this._pageCSSLoading[page]}, {
		floater : obj._mapFloater}, function(data, callback){
			obj._mapFloater.setDefaultHtml(data);
			obj._mapFloater.showDefault(callback);
	});
}

function homepageCtrl_destroyContentPage(baseFloaterName){
	for (var i = 0; i < obj._contentPages.length; i ++){
		if (obj._contentPages[0] == baseFloaterName){
			obj._contentPages[1].destroy();
			obj._contentPages[2].destroy();
			obj._contentPages.splice(i, 1);
			break;
		}
	}
}

function homepageCtrl_loadContentPage(baseFloaterName, page, parameter){
	var obj = this;
	var pageObj = obj.getContentPage_p(baseFloaterName);
	var floater;
	if (pageObj == null){
		floater = new GlobalFloater(parameter);
		pageObj = [baseFloaterName, floater, null];
		obj._contentPages.push(pageObj);
	}  else {
		floater = pageObj[1];
		pageObj[2].destroy();
	}
	floater.showLoading();
	pageObj[2] = new PageLoader("/homepage/getMainPage", this._pageClassName[page], 
		{postData : {"page" : page}, javaScript : this._pageJsLoading[page], CSS : this._pageCSSLoading[page]}, {
		floater : floater, homepageControl : obj}, function(data, callback){
			floater.setDefaultHtml(data);
			floater.showDefault(callback);
	});
}

function homepageCtrl_updateLoginState(){
	this.loadLeftNavPage();
	if (globalCtrl.loginState() == "loggedIn"){
		this._leftNavFloater.setDefaultLook({left : "100px", top : "100px", width : "200px", height : "120px"});
	}  else {
		this._leftNavFloater.setDefaultLook({left : "100px", top : "100px", width : "200px", height : "80px"});
	}  
}

function homepageCtrl_getContentPage_p(page){
	var obj = this;
	for (var i = 0; i < obj._contentPages.length; i ++)
		if (obj._contentPages[i][0] == page) return obj._contentPages[i];
	return null;
}

if (dojo.getObject("homepageCtrl") == null) homepageCtrl = new HomepageCtrl();
$(document).ready(function(){
	homepageCtrl.init();
}); 
